package com.hx.demo.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hx.demo.domain.User;
import com.hx.demo.mapper.UserMapper;
import com.hx.demo.service.IUserSeervice;
import com.hx.demo.util.AjaxResult;
import com.hx.demo.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.spring5.webflow.view.AjaxEnabledView;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;

import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private IUserSeervice userSeervice;


    /**
     * 跳转到用户列表页面
     * @return
     */
    @RequestMapping(path = "/toList")
    public String toList(){
        return "/admin/user/user_list";
    }

    /**
     * 跳转到用户添加页面
     * @return
     */
    @GetMapping("/toUserAdd")
    public String uadd(){
        return "/admin/user/user_add";
    }
    /**
     * 跳转到修改密码页面
     * @return
     */
    @GetMapping("/updPwd")
    public String updPwd(){
        return "/admin/user/user-password";
    }
    /**
     * 跳转到修改基本资料页面
     * @return
     */
    @RequestMapping("/toSetting")
    public String toSetting(Model model) {
        User user = SecurityUtils.getUser();
        user = userSeervice.selectByUserName(user.getUsername());
        model.addAttribute("user",user);
        return "/admin/user/user_setting";
    }
    /**
     * 修改基本资料
     * @return
     */
    @PostMapping("/updateUserSetting")
    @ResponseBody
    public AjaxResult updateUserSetting(@RequestBody User user) {
        int row = userSeervice.updateUser(user);
        return AjaxResult.toAjax(row);
    }
    /**
     * 获取用户列表的数据
     * @return
     */
    @RequestMapping(path = "/list")
    @ResponseBody
    public AjaxResult list(int page,int limit,String searchParams){

        log.info("{}-{}-{}",page,limit,searchParams);
        //讲请求参数转化json对象
        User userParam = JSONObject.parseObject(searchParams,User.class);

        Page<User> pageObj = new Page<>(page, limit);
        Page<User> userPage = userSeervice.selectUserListByPage(pageObj,userParam);

        return AjaxResult.tableList(userPage.getRecords(),userPage.getTotal());
    }

    /**
     * 根据uid删除用户
     * @param uid
     * @return
     */
    @PostMapping("/del/{uid}")
    @ResponseBody
    public AjaxResult delUserById(@PathVariable("uid") Integer uid){
        int row = userSeervice.deleteUserById(uid);
        return AjaxResult.toAjax(row);
    }
    /**
     * 添加User
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody User user) {

        User userFlag = userSeervice.selectByUserName(user.getUsername());

        if (ObjectUtil.isNotNull(userFlag)){
            return AjaxResult.error("用户已存在,请更换用户名");
        }

        int row = userSeervice.insertUser(user);
        return AjaxResult.toAjax(row);
    }
    /**
     * 根据uid提交信息到用户修改页面
     * @param uid
     * @return
     */
    @RequestMapping("/toEdit/{uid}")
    public String toEdit(@PathVariable("uid") Integer uid, Model model) {

        User user = userSeervice.selectById(uid);
        model.addAttribute("user",user);
        log.info("{}-{}",uid,model);
        return "/admin/user/user_edit";
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody User user) {
        int row = userSeervice.updateUser(user);
        return AjaxResult.toAjax(row);
    }

    @PostMapping("/updPw")
    @ResponseBody
    public AjaxResult updatePassword(@RequestBody Map<String,String> requestMap) {

        User user = SecurityUtils.getUser();

        String old_password = requestMap.get("old_password");
        String new_password = requestMap.get("new_password");
        String again_password = requestMap.get("again_password");

        if (StringUtils.isEmpty(old_password) || StringUtils.isEmpty(new_password) || StringUtils.isEmpty(again_password)){
            return AjaxResult.error("信息不能为空");
        }

        //校验旧密码是否正确
        if (!user.getPassword().equals(old_password)){
            return AjaxResult.error("旧密码错误");
        }

        //判断两次密码是否一致
        if (!new_password.equals(again_password)){
            return AjaxResult.error("两次密码不一致");
        }

        //更新密码
        user.setPassword(new_password);
        int row = userSeervice.updateUser(user);

        return AjaxResult.toAjax(row);
    }

}
